package com.xoa.dev.keyYouth.service;


import com.xoa.dev.keyYouth.dao.CVehiclesMapper;
import com.xoa.dev.keyYouth.model.CVehicles;
import com.xoa.dev.pz.dao.PzPersonMapper;
import com.xoa.dev.pz.model.PzPersonWithBLOBs;
import com.xoa.model.users.Users;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CVehiclesService {

    @Resource
    private CVehiclesMapper cVehiclesMapper;

    @Resource
    private PzPersonMapper pzPersonMapper;

    public ToJson addCVehicles(HttpServletRequest request, CVehicles record){
        ToJson json = new ToJson();
        try {
            //从session中获取当前登录人的信息
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            record.setCreatPerson(users.getUserName());
            record.setCreatDepart(users.getDeptName());
            record.setCreatData(new Date());
            record.setValidFlag("0");
            if(record.getpId()!=null && !"".equals(record.getpId())){
            List<PzPersonWithBLOBs> pzPeople = pzPersonMapper.cVehiclesId(Integer.valueOf(record.getpId()));
            for (PzPersonWithBLOBs f:pzPeople) {
                record.setpGrid(f.getGridName());
                record.setpCardnum(f.getpCradId());
                record.setpLivingaddress(f.getpAddress());
                }
            }
            int count = cVehiclesMapper.insertSelective(record);
            if (count>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        }catch (Exception e){
            json.setMsg("no");
            json.setFlag(1);
        }
        return json;
    }


    public ToJson selectCVehicles(Integer page, Integer pageSize, Boolean useFlag, CVehicles record) {
        ToJson json = new ToJson();
        Map<String,Object> map = new HashMap<String, Object>();
        PageParams pageParams = new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        map.put("page",pageParams);
        map.put("zz",record);
        try {
            List<CVehicles> list = cVehiclesMapper.lists(map);
            for (CVehicles l : list) {
                if(l.getpId()!=null && !"".equals(l.getpId())) {
                    List<PzPersonWithBLOBs> pzPeople = pzPersonMapper.cVehiclesId(Integer.parseInt(l.getpId()));
                    for (PzPersonWithBLOBs f : pzPeople) {
                        l.setpName(f.getpName());
                    }
                }
            }
            if (list != null) {
                json.setTotleNum(pageParams.getTotal());
                json.setObj(list);
                json.setMsg("ok");
                json.setFlag(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setFlag(1);
        }
        return json;
    }
    public ToJson updateCVehicles(HttpServletRequest request,CVehicles record) {
        ToJson json = new ToJson();
        try {
            //从session中获取当前登录人的信息
            Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            record.setUpdateDepart(users.getDeptName());
            record.setUpdatePerson(users.getUserName());
            record.setUpdateData(new Date());
            if(record.getpId()!=null && !"".equals(record.getpId())){
                List<PzPersonWithBLOBs> pzPeople = pzPersonMapper.cVehiclesId(Integer.valueOf(record.getpId()));
                for (PzPersonWithBLOBs f:pzPeople) {
                    record.setpGrid(f.getGridName());
                    record.setpCardnum(f.getpCradId());
                    record.setpLivingaddress(f.getpAddress());
                }
            }
            cVehiclesMapper.updateByPrimaryKeySelective(record);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
        }
        return json;
    }
    public ToJson selectInofCVehicles(Integer id) {
        ToJson json = new ToJson();
        try {
            CVehicles pAdolescent = cVehiclesMapper.selectByPrimaryKey(id);
            if(pAdolescent.getpId()!=null && !"".equals(pAdolescent.getpId())) {
                List<PzPersonWithBLOBs> pzPeople = pzPersonMapper.cVehiclesId(Integer.parseInt(pAdolescent.getpId()));
                for (PzPersonWithBLOBs f : pzPeople) {
                    pAdolescent.setpName(f.getpName());
                }
            }
            json.setObject(pAdolescent);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
        }
        return json;
    }
    public ToJson deleteCVehicles(HttpServletRequest request, Integer id) {
        ToJson json = new ToJson();
        try {
            cVehiclesMapper.deleteByPrimaryKey(id);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
        }
        return json;
    }


    public ToJson names(){
        ToJson json = new ToJson();
        try{
            List<PzPersonWithBLOBs> pzPeople = pzPersonMapper.cVehiclesId(null);
            json.setObject(pzPeople);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
        }
        return json;
    }
    /**
     * 批量删除
     *
     */
    public ToJson delete(String ids){
        int count = 0;
        try {
            if (!StringUtils.checkNull(ids)) {
                String[] split = ids.split(",");
                if (split != null && split.length > 0) {
                    count = cVehiclesMapper.deleteAll(split);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ToJson(1,"出现异常");
        }
        if (count > 0){
            return new ToJson(0,"删除成功");
        }
        return new ToJson(1,"删除失败");
    }

}
